//
//  SubscribeViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/7/1.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "SubscribeViewController.h"
#import "FirstDetailCell.h"
#import "DetailHeadModel.h"
#import "DBManager.h"
#import "DetailViewController.h"
#import "UIImageView+WebCache.h"




@interface SubscribeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_favoriteArr;
    NSMutableArray *_deleteArr;
    UIButton *_button;
}

@end

@implementation SubscribeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showUI];
    //[self DeleteButton];
  
   // self.view.backgroundColor=  [UIColor darkGrayColor ];
    self.navigationController.title = @"订阅历史";
}



-(void)showUI{
    self.dataArr = [[NSMutableArray alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenSize.width , kScreenSize.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:@"btn_downloadalbum_delete_n"] forState:UIControlStateNormal];
    

    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
        self.editButtonItem.image = [UIImage imageNamed:@"btn_downloadalbum_delete_n"];
    UIBarButtonItem *item  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_downloadalbum_delete_n"] style:UIBarButtonItemStylePlain target:self action:@selector(itemClick:)];
    self.navigationItem.rightBarButtonItem = item;
    
    
    
    
    
    
        [self.tableView registerNib:[UINib nibWithNibName:@"FirstDetailCell" bundle:nil] forCellReuseIdentifier:@"Detail"];
    [self.view addSubview:self.tableView];
    
    
    NSArray *arr = [[DBManager sharedManager]readModels];
    
    _favoriteArr = [[NSMutableArray alloc] init];
    for (DetailHeadModel *model in arr) {
        [_favoriteArr addObject:model];
    }
    
    
    [self.tableView reloadData];
    
    
    
}
- (void)itemClick:(UIBarButtonItem *)item {
    BOOL isE = self.tableView.editing;
    [self.tableView setEditing:!isE animated:YES];
}

#pragma mark UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _favoriteArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FirstDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Detail" forIndexPath:indexPath];
   
    DetailHeadModel *model = _favoriteArr[indexPath.row];
    cell.titleLabel.text = model.title;
    [cell.coverSmall sd_setImageWithURL:[NSURL URLWithString:model.coverLarge] placeholderImage:nil];
    cell.tracksLabel.text = [NSString stringWithFormat:@"节目数:%@",model.tracks];
    
    
    return cell;
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailHeadModel *model = _favoriteArr[indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.albumId = model.albumId;
    [self.navigationController pushViewController:detail animated:YES];
 
    

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
       [self.tableView setEditing:!self.tableView.editing animated:YES];
    
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
   
       return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
           return UITableViewCellEditingStyleDelete;
   
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
   // NSMutableArray *arr = _favoriteArr[indexPath.row];
    //删除指定索引
    
    
    
    DetailHeadModel *model = _favoriteArr[indexPath.row];
    [_favoriteArr removeObjectAtIndex:indexPath.row];
    [[DBManager sharedManager]deleteModelForAppId:[NSString stringWithFormat:@"%@",model.albumId]];
    
    [self.tableView reloadData];
    [self.tableView setEditing:NO];
     

}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
 return @"删除";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
